/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.elcom.emocbc.livescreendb.dao;

import com.elcom.util.Constant;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.mchange.v2.c3p0.DataSources;
import java.io.File;
import java.io.FileReader;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Administrator
 */
public class DbAccess {

    private static DbAccess instance = new DbAccess();
    private DataSource dataSource;
    private static Logger logger;

    private DbAccess() {
        logger = LoggerFactory.getLogger(DbAccess.class);
        dataSource = setupDataSource();
    }

    public static synchronized DbAccess getInstance() {
        if (instance == null) {
            return new DbAccess();
        } else {
            return instance;
        }
    }

    public Connection getConnection() throws SQLException {
        if (dataSource == null) {
            dataSource = setupDataSource();
        }

        Connection conn = dataSource.getConnection();
        if (conn != null && !conn.isClosed()) {
            conn.setAutoCommit(false);
        }
        
        return conn;
    }

    private DataSource setupDataSource() {
        try {
            logger.info("Setting up datasource");
            //            String jdbcUrl = "jdbc:mysql://localhost:3306/cbcdb?zeroDateTimeBehavior=convertToNull";
            //            String userName = "root";
            //            String password = "123456";
            //            String classDriverName = "com.mysql.jdbc.Driver";
            Properties properties = new Properties();
            properties.load(new FileReader(new File(Constant.CONFIG_FOLDER + "livescreen-connection-pool.props")));
            String jdbcUrl = properties.getProperty("jdbcUrl");
            String classDriverName = properties.getProperty("classDriverName");

            ComboPooledDataSource cpds = new ComboPooledDataSource();
            cpds.setDriverClass(classDriverName); //loads the jdbc driver            
            cpds.setJdbcUrl(jdbcUrl);
            cpds.setProperties(properties);
            return cpds;
        } catch (Exception ex) {
            logger.error("", ex);
            return null;
        }
    }

    public static void closeConnection(Connection cn) {
        if (cn != null) {
            try {
                if (!cn.isClosed()) {
                    cn.close();
                }
            } catch (SQLException ex) {
                logger.error("", ex);
            }
        }
    }

    public static void commitTransaction(Connection cn) {
        try {
            cn.commit();
        } catch (SQLException ex) {
            logger.error("", ex);
        }
    }

    public static void rollBackTransaction(Connection cn) {
        try {
            cn.rollback();
        } catch (SQLException ex) {
            logger.error("", ex);
        }
    }

    public static void closeConn(Connection conn) {
        if (conn != null) {
            try {
                conn.close();
            } catch (Exception ex) {
                logger.error("", ex);
            }
        }
    }

    public static void closePreparedStatement(PreparedStatement ps) {
        if (ps != null) {
            try {
                ps.close();
            } catch (Exception ex) {
                logger.error("", ex);
            }
        }
    }

    public static void closeCallableStatement(CallableStatement cs) {
        if (cs != null) {
            try {
                cs.close();
            } catch (Exception ex) {
                logger.error("", ex);
            }
        }
    }

    public static void closeResultSet(ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException ex) {
                logger.error("", ex);
            }
        }
    }

    public static void closeStatement(Statement stmt) {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (Exception ex) {
                logger.error("", ex);
            }
        }
    }

    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        DataSources.destroy(dataSource);
        logger.debug("DB Connection Pool destroyed");
    }
    
    
}
